package com.itheima.canal.listener;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.itheima.canal.config.RabbitMQConfig;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.ListenPoint;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


@CanalEventListener//声明当前的类是canal的监听类
public class BusinessListener {
    @Autowired
    public RabbitTemplate rabbitTemplate;

    /**
     *
     * @param entryType 当前操作数据库的类型
     * @param rowData 当前操作数据库的数据
     */
    @ListenPoint(schema = "changgou_business",table = "tb_ad" )
    public void adUpdate(CanalEntry.EntryType entryType,CanalEntry.RowData rowData){
        /*System.out.println("广告数据开始发生变化");
        //获取改变之前的数据
        rowData.getBeforeColumnsList().forEach((c)-> System.out.println("改变前的数据为:"+c));
        //获取改变之后的数据
        rowData.getAfterColumnsList().forEach((c)-> System.out.println("改变后的数据为:"+c));*/
        List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();
        for (CanalEntry.Column column : afterColumnsList) {
            if ("position".equals(column.getName())){
                System.out.println("发送最新数据:"+column.getValue());
                //发送最新数据到MQ
                rabbitTemplate.convertAndSend("",RabbitMQConfig.AD_UPDATE_QUEUE,column.getValue());
            }
        }
    }
}
